package com.ly.hotel.view.common;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ly.hotel.R;

public class CustomDialog extends Dialog {


	public CustomDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String title;
		private String message;
		private String firstButtonText;
		private String secondButtonText;
		private String thirdButtonText;
		private String fourthButtonText;

		private View contentView;
		private OnClickListener firstButtonClickListener;
		private OnClickListener secondButtonClickListener;
		private OnClickListener thirdButtonClickListener;
		private OnClickListener fourthButtonClickListener;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 * 
//		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 * 
		 * @param title
		 * @return
		 */

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		public Builder setFirstButton(String positiveButtonText, OnClickListener listener) {
			this.firstButtonText = positiveButtonText;
			this.firstButtonClickListener = listener;
			return this;
		}

		public Builder setSecondButton(String negativeButtonText, OnClickListener listener) {
			this.secondButtonText = negativeButtonText;
			this.secondButtonClickListener = listener;
			return this;
		}

		public Builder setThirdButton(String meButtonText, OnClickListener listener) {
			this.thirdButtonText = meButtonText;
			this.thirdButtonClickListener = listener;
			return this;
		}

		public Builder setFourthButton(String meButtonText, OnClickListener listener) {
			this.fourthButtonText = meButtonText;
			this.fourthButtonClickListener = listener;
			return this;
		}
		
		public CustomDialog create() {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
			View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title
			((TextView) layout.findViewById(R.id.title)).setText(title);
			// set the confirm button
			if (firstButtonText != null) {
				final Button firstBtn = (Button) layout.findViewById(R.id.firstButton);
				firstBtn.setText(firstButtonText);
				if (firstButtonClickListener != null) {
					firstBtn.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							firstButtonClickListener.onClick(dialog, 1);
						}
					});
				}
				firstBtn.post(new Runnable(){
					public void run(){
						firstBtn.requestFocus();
						firstBtn.requestFocusFromTouch();
					}
				});
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.firstButton).setVisibility(View.GONE);
			}
			// set the cancel button
			if (secondButtonText != null) {
				((Button) layout.findViewById(R.id.secondButton)).setText(secondButtonText);
				if (secondButtonClickListener != null) {
					((Button) layout.findViewById(R.id.secondButton)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							secondButtonClickListener.onClick(dialog, 2);
						}
					});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.secondButton).setVisibility(View.GONE);
			}

			if (thirdButtonText != null) {
				((Button) layout.findViewById(R.id.thirdButton)).setText(thirdButtonText);
				if (thirdButtonClickListener != null) {
					((Button) layout.findViewById(R.id.thirdButton)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							thirdButtonClickListener.onClick(dialog, 3);
						}
					});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.thirdButton).setVisibility(View.GONE);
			}

			if (fourthButtonText != null) {
				((Button) layout.findViewById(R.id.fourthButton)).setText(fourthButtonText);
				if (fourthButtonClickListener != null) {
					((Button) layout.findViewById(R.id.fourthButton)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							fourthButtonClickListener.onClick(dialog, 3);
						}
					});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.fourthButton).setVisibility(View.GONE);
			}
			
			// set the content message
			if (message != null) {
				((TextView) layout.findViewById(R.id.message)).setText(message);
			} else if (contentView != null) {
				// if no message set
				// add the contentView to the dialog body
				((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
				((LinearLayout) layout.findViewById(R.id.content)).addView(contentView, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			} else if (message == null){
				layout.findViewById(R.id.content).setVisibility(View.GONE);
			}
			dialog.setContentView(layout);
			return dialog;
		}
	}
}
